package com.xhd.db;

import java.io.OutputStream;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;

import com.xhd.base.util.xBaseObj;
import com.xhd.exception.DatabaseException;
import com.xhd.pagination.DataPaging;

public abstract class BizQueryImpl
implements BizQuery
{
protected boolean bCheckArea = true;
protected static final BizObjStoreListener bizListener = BizObjStoreListener.getInstance();

public String getTable(String paramString)
{
  if (!(this.bCheckArea))
    return null;
  return SQLAnalyzer.getTable(paramString);
}

protected abstract DBControl getControl(String paramString)
  throws DatabaseException;

protected abstract xBaseObj getCreator();

public BizQueryImpl(boolean paramBoolean)
{
  this.bCheckArea = paramBoolean;
}

public Vector multiRowSelect(Object paramObject)
  throws DatabaseException
{
  if (paramObject instanceof DBQuery)
  {
	  DBQuery  localObject = (DBQuery)paramObject;
	  DBControl  localDBControl = getControl(((DBQuery)localObject).getMainTable());
    bizListener.onQuery(getCreator(), (DBQuery)localObject);
    return localDBControl.multiRowSelect(((DBQuery)localObject).jm442(localDBControl.getDBType()), ((DBQuery)localObject).getMaxCount(), ((DBQuery)localObject).getPage());
  }
  if (paramObject instanceof DBSQL)
  {
	DBSQL  localObject = (DBSQL)paramObject;
   DBControl   localDBControl = getControl(getTable(((DBSQL)localObject).getSQL()));
    bizListener.onQuery(getCreator(), (DBSQL)localObject, localDBControl.getDBType());
    return localDBControl.multiRowSelect((DBSQL)localObject, 0, 0);
  }
  Object localObject = paramObject.toString();
  DBControl localDBControl = getControl(getTable((String)localObject));
  localObject = bizListener.onQuery(getCreator(), (String)localObject, localDBControl.getDBType());
  return ((Vector)localDBControl.multiRowSelect((String)localObject, 0, 0));
}

public Vector multiRowSelect(String paramString, int paramInt)
  throws DatabaseException
{
  DBControl localDBControl = getControl(getTable(paramString));
  paramString = bizListener.onQuery(getCreator(), paramString, localDBControl.getDBType());
  return localDBControl.multiRowSelect(paramString, paramInt, 0);
}

public Vector multiRowSelect(DBSQL paramDBSQL, int paramInt)
  throws DatabaseException
{
  DBControl localDBControl = getControl(getTable(paramDBSQL.getSQL()));
  bizListener.onQuery(getCreator(), paramDBSQL, localDBControl.getDBType());
  return localDBControl.multiRowSelect(paramDBSQL, paramInt, 0);
}

public Vector pageSelect(Object paramObject, DataPaging paramDataPaging)
  throws DatabaseException
{
  if ((paramDataPaging == null) || (paramDataPaging.getPageSize() < 1))
    return multiRowSelect(paramObject);
  DBControl localDBControl = null;
  Vector localVector = null;
  DBSQL localDBSQL = new DBSQL();
  Object localObject1;
  Object localObject2;
  if (paramObject instanceof DBQuery)
  {
    localObject1 = (DBQuery)paramObject;
    localDBControl = getControl(((DBQuery)localObject1).getMainTable());
    bizListener.onQuery(getCreator(), (DBQuery)localObject1);
    localObject2 = ((DBQuery)localObject1).jm442(localDBControl.getDBType());
    localVector = localDBControl.query((DBSQL)localObject2, paramDataPaging.getQueryStart(), paramDataPaging.getQueryLimit());
    localDBSQL.copy((DBSQL)localObject2);
  }
  else if (paramObject instanceof DBSQL)
  {
    localObject1 = (DBSQL)paramObject;
    localDBControl = getControl(getTable(((DBSQL)localObject1).getSQL()));
    bizListener.onQuery(getCreator(), (DBSQL)localObject1, localDBControl.getDBType());
    localVector = localDBControl.query((DBSQL)localObject1, paramDataPaging.getQueryStart(), paramDataPaging.getQueryLimit());
    localDBSQL.copy((DBSQL)localObject1);
  }
  else
  {
    localObject1 = paramObject.toString();
    localDBControl = getControl(getTable((String)localObject1));
    localObject1 = bizListener.onQuery(getCreator(), (String)localObject1, localDBControl.getDBType());
    localVector = localDBControl.query((String)localObject1, paramDataPaging.getQueryStart(), paramDataPaging.getQueryLimit());
    localDBSQL.setSQL((String)localObject1);
  }
  if ((localVector != null) && (paramDataPaging.getCount() == 0))
    if (localVector.size() < paramDataPaging.getQueryLimit())
    {
      paramDataPaging.setCount(localVector.size() + paramDataPaging.getQueryStart());
    }
    else
    {
      localObject1 = paramDataPaging.getCountSQL();
      if (localObject1 != null)
      {
        localObject1 = bizListener.onQuery(getCreator(), (String)localObject1, localDBControl.getDBType());
        paramDataPaging.setCount(localDBControl.intSelect((String)localObject1));
      }
      else
      {
        localObject2 = localDBSQL.getSQL();
        int i = SQLAnalyzer.getKeyIndex((String)localObject2, " order by ");
        if (i > 0)
          localObject2 = ((String)localObject2).substring(0, i);
        localDBSQL.setSQL("Select count(1) From (");
        localDBSQL.appendSQL((String)localObject2);
        localDBSQL.appendSQL(") v");
        paramDataPaging.setCount(localDBControl.intSelect(localDBSQL));
      }
    }
  return ((Vector)(Vector)localVector);
}

public Hashtable oneRowSelect(Object paramObject)
  throws DatabaseException
{
  if (paramObject instanceof DBQuery)
  {
	  DBQuery localObject = (DBQuery)paramObject;
	  DBControl  localDBControl = getControl(((DBQuery)localObject).getMainTable());
    bizListener.onQuery(getCreator(), (DBQuery)localObject);
    return localDBControl.oneRowSelect(((DBQuery)localObject).jm442(localDBControl.getDBType()));
  }
  if (paramObject instanceof DBSQL)
  {
	  DBSQL   localObject = (DBSQL)paramObject;
	  DBControl  localDBControl = getControl(getTable(((DBSQL)localObject).getSQL()));
    bizListener.onQuery(getCreator(), (DBSQL)localObject, localDBControl.getDBType());
    return localDBControl.oneRowSelect((DBSQL)localObject);
  }
  Object localObject = paramObject.toString();
  DBControl localDBControl = getControl(getTable((String)localObject));
  localObject = bizListener.onQuery(getCreator(), (String)localObject, localDBControl.getDBType());
  return ((Hashtable)localDBControl.oneRowSelect((String)localObject));
}

public Vector oneListSelect(Object paramObject)
  throws DatabaseException
{
  if (paramObject instanceof DBQuery)
  {
	  DBQuery  localObject = (DBQuery)paramObject;
	  DBControl localDBControl = getControl(((DBQuery)localObject).getMainTable());
    bizListener.onQuery(getCreator(), (DBQuery)localObject);
    return localDBControl.oneListSelect(((DBQuery)localObject).jm442(localDBControl.getDBType()), ((DBQuery)localObject).getMaxCount());
  }
  if (paramObject instanceof DBSQL)
  {
	  DBSQL  localObject = (DBSQL)paramObject;
	  DBControl localDBControl = getControl(getTable(((DBSQL)localObject).getSQL()));
    bizListener.onQuery(getCreator(), (DBSQL)localObject, localDBControl.getDBType());
    return localDBControl.oneListSelect((DBSQL)localObject, 0);
  }
  Object localObject = paramObject.toString();
  DBControl localDBControl = getControl(getTable((String)localObject));
  localObject = bizListener.onQuery(getCreator(), (String)localObject, localDBControl.getDBType());
  return ((Vector)localDBControl.oneListSelect((String)localObject, 0));
}

public int intSelect(Object paramObject)
  throws DatabaseException
{
  if (paramObject instanceof DBQuery)
  {
	DBQuery localObject = (DBQuery)paramObject;
	DBControl localDBControl = getControl(((DBQuery)localObject).getMainTable());
    bizListener.onQuery(getCreator(), (DBQuery)localObject);
    return localDBControl.intSelect(((DBQuery)localObject).jm442(localDBControl.getDBType()));
  }
  if (paramObject instanceof DBSQL)
  {
	DBSQL localObject = (DBSQL)paramObject;
	DBControl localDBControl = getControl(getTable(((DBSQL)localObject).getSQL()));
    bizListener.onQuery(getCreator(), (DBSQL)localObject, localDBControl.getDBType());
    return localDBControl.intSelect((DBSQL)localObject);
  }
  Object localObject = paramObject.toString();
  DBControl localDBControl = getControl(getTable((String)localObject));
  localObject = bizListener.onQuery(getCreator(), (String)localObject, localDBControl.getDBType());
  return localDBControl.intSelect((String)localObject);
}

public String strSelect(Object paramObject)
  throws DatabaseException
{
  if (paramObject instanceof DBQuery)
  {
	DBQuery localObject = (DBQuery)paramObject;
	DBControl localDBControl = getControl(((DBQuery)localObject).getMainTable());
    bizListener.onQuery(getCreator(), (DBQuery)localObject);
    return localDBControl.strSelect(((DBQuery)localObject).jm442(localDBControl.getDBType()));
  }
  if (paramObject instanceof DBSQL)
  {
	 DBSQL localObject = (DBSQL)paramObject;
	 DBControl localDBControl = getControl(getTable(((DBSQL)localObject).getSQL()));
    bizListener.onQuery(getCreator(), (DBSQL)localObject, localDBControl.getDBType());
    return localDBControl.strSelect((DBSQL)localObject);
  }
  Object localObject = paramObject.toString();
  DBControl localDBControl = getControl(getTable((String)localObject));
  localObject = bizListener.onQuery(getCreator(), (String)localObject, localDBControl.getDBType());
  return ((String)localDBControl.strSelect((String)localObject));
}

public Vector getCall(DBQuery paramDBQuery)
  throws DatabaseException
{
  DBControl localDBControl = getControl(paramDBQuery.getMainTable());
  return localDBControl.getCall(paramDBQuery.getMainTable(), paramDBQuery.getCallParams(), paramDBQuery.getResultType());
}

public Vector getCall(String paramString, List paramList, int paramInt)
  throws DatabaseException
{
  DBControl localDBControl = getControl(paramString);
  return localDBControl.getCall(paramString, paramList, paramInt);
}

public Vector[] rptQuery(DBQuery paramDBQuery)
  throws DatabaseException
{
  if (paramDBQuery.isProcedure())
  {
	  DBControl   localDBControl = getControl(paramDBQuery.getMainTable());
    return localDBControl.getCall2(paramDBQuery.getMainTable(), paramDBQuery.getCallParams(), paramDBQuery.getResultType());
  }
  DBControl localDBControl = getControl(paramDBQuery.getMainTable());
  bizListener.onQuery(getCreator(), paramDBQuery);
  return localDBControl.multiRowSelect2(paramDBQuery.jm442(localDBControl.getDBType()), paramDBQuery.getMaxCount());
}

public Vector selectObjectData(Object paramObject)
  throws DatabaseException
{
  if (paramObject instanceof DBQuery)
  {
	  DBQuery localObject = (DBQuery)paramObject;
	  DBControl localDBControl = getControl(((DBQuery)localObject).getMainTable());
    bizListener.onQuery(getCreator(), (DBQuery)localObject);
    return localDBControl.selectObjectData(((DBQuery)localObject).jm442(localDBControl.getDBType()));
  }
  if (paramObject instanceof DBSQL)
  {
	  DBSQL localObject = (DBSQL)paramObject;
	  DBControl localDBControl = getControl(getTable(((DBSQL)localObject).getSQL()));
    bizListener.onQuery(getCreator(), (DBSQL)localObject, localDBControl.getDBType());
    return localDBControl.selectObjectData((DBSQL)localObject);
  }
  Object localObject = paramObject.toString();
  DBControl localDBControl = getControl(getTable((String)localObject));
  localObject = bizListener.onQuery(getCreator(), (String)localObject, localDBControl.getDBType());
  return ((Vector)localDBControl.selectObjectData((String)localObject));
}

public PreparedStatement getPrepareStatement(String paramString)
  throws DatabaseException
{
  DBControl localDBControl = getControl(getTable(paramString));
  paramString = bizListener.onQuery(getCreator(), paramString, localDBControl.getDBType());
  return getControl(getTable(paramString)).getPrepareStatement(paramString);
}

public ResultSet getResultSet(String paramString)
  throws DatabaseException
{
  DBControl localDBControl = getControl(getTable(paramString));
  paramString = bizListener.onQuery(getCreator(), paramString, localDBControl.getDBType());
  return getControl(getTable(paramString)).getResultSet(paramString);
}

public CallableStatement getCallableStatement(String paramString)
  throws DatabaseException
{
  return getControl(getTable(paramString)).getCallableStatement(paramString);
}

public Hashtable getColumns(String paramString)
  throws DatabaseException
{
  BizObjFields localBizObjFields = getControl(paramString).getTableFields(paramString);
  if (localBizObjFields != null)
  {
    Hashtable localHashtable = new Hashtable();
    localBizObjFields.toMapJavaType(localHashtable);
    return localHashtable;
  }
  return null;
}

public Hashtable getColumnTypes(String paramString)
  throws DatabaseException
{
  return getControl(paramString).getColumnTypes(paramString);
}

public int streamSelect(Object paramObject, OutputStream paramOutputStream)
  throws DatabaseException
{
  if (paramObject instanceof DBQuery)
  {
	  DBQuery localObject = (DBQuery)paramObject;
	  DBControl localDBControl = getControl(((DBQuery)localObject).getMainTable());
    bizListener.onQuery(getCreator(), (DBQuery)localObject);
    return localDBControl.streamSelect(((DBQuery)localObject).jm442(localDBControl.getDBType()), paramOutputStream);
  }
  if (paramObject instanceof DBSQL)
  {
	  DBSQL localObject = (DBSQL)paramObject;
    DBControl  localDBControl = getControl(getTable(((DBSQL)localObject).getSQL()));
    bizListener.onQuery(getCreator(), (DBSQL)localObject, localDBControl.getDBType());
    return localDBControl.streamSelect((DBSQL)localObject, paramOutputStream);
  }
  Object localObject = paramObject.toString();
  DBControl localDBControl = getControl(getTable((String)localObject));
  localObject = bizListener.onQuery(getCreator(), (String)localObject, localDBControl.getDBType());
  return localDBControl.streamSelect((String)localObject, paramOutputStream);
}
}